package com.gree.changying.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.gree.changying.R;
import com.gree.changying.server.utils.NToast;
import com.gree.changying.utils.StringUtil;

/**
 * Create by 游德禄 on 2019-9-7
 */
public class ProgressDialog extends Dialog {

    private static com.gree.changying.widget.ProgressDialog loadDialog;
    private Context context;
    private boolean canNotCancel;
    private String tipMsg;

    private TextView mShowMessage;

    public ProgressDialog(final Context ctx, boolean canNotCancel, String tipMsg) {
        super(ctx);
        this.context = ctx;
        this.canNotCancel = canNotCancel;
        this.tipMsg = tipMsg;
        try {
            this.getContext().setTheme(android.R.style.Theme_InputMethod);
        }catch (Exception e){

        }
        setContentView(R.layout.dialog_progress);

        if (!StringUtil.isEmpty(this.tipMsg)) {
            mShowMessage =  findViewById(R.id.show_message);
            mShowMessage.setVisibility(View.VISIBLE);
            mShowMessage.setText(this.tipMsg);
        }

        Window window = getWindow();
        WindowManager.LayoutParams attributesParams = window.getAttributes();
        attributesParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        attributesParams.dimAmount = 0.5f;
        window.setAttributes(attributesParams);
        window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (canNotCancel) {
                NToast.shortToast(getContext(),tipMsg);
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public static void show(Context context) {
        show(context, null, false);
    }

    public static void show(Context context, String message) {
        show(context, message, false);
    }
    public com.gree.changying.widget.ProgressDialog setMsg(String msg){
        if (!StringUtil.isEmpty(msg)) {
            mShowMessage =  findViewById(R.id.show_message);
            mShowMessage.setVisibility(View.VISIBLE);
            mShowMessage.setText(msg);
        }
        return this;
    }

    private static void show(Context context, String message, boolean isCancel) {
        if(context==null){
            return;
        }
        if (context instanceof Activity) {
            if (((Activity) context).isFinishing()) {
                return;
            }
        }
        if (loadDialog != null && loadDialog.isShowing()) {
            return;
        }
        loadDialog = new com.gree.changying.widget.ProgressDialog(context, isCancel, message);
        loadDialog.show();
    }
    public static void disMiss() {
        if(loadDialog==null||loadDialog.context==null){
            return;
        }
        try {
            if (loadDialog.context instanceof Activity) {
                if (((Activity) loadDialog.context).isFinishing()) {
                    loadDialog = null;
                    return;
                }
            }

            if (loadDialog != null && loadDialog.isShowing()) {
                Context loadContext = loadDialog.getContext();
                if (loadContext != null && loadContext instanceof Activity) {
                    if (((Activity) loadContext).isFinishing()) {
                        loadDialog = null;
                        return;
                    }
                }
                loadDialog.dismiss();
                loadDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            loadDialog = null;
        }
    }
}
